﻿using ICSharpCode.AvalonEdit;
using System.Windows;
using System.Windows.Interactivity;

namespace Fluffy.Text.Behaviors
{
	public class TextEditorInitializedBehavior : Behavior<TextEditor>
	{
		public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
			"ViewModel", typeof(TextDocumentViewModel), typeof(TextEditorInitializedBehavior), new PropertyMetadata(null, OnViewModelChanged));

		private static void OnViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			TextDocumentViewModel viewModel = e.NewValue as TextDocumentViewModel;
			if (viewModel != null)
			{
				viewModel.TextEditor = ((TextEditorInitializedBehavior) d).AssociatedObject;
			}
		}

		public TextDocumentViewModel ViewModel
		{
			get { return (TextDocumentViewModel)GetValue(ViewModelProperty); }
			set { SetValue(ViewModelProperty, value); }
		}
	}
}
